#pragma once
#include <vector>
#include <string>
#include "macros.h"
#include "sqlite3.h"

class DBResult {
public:
	std::vector< std::vector<std::string> > data;
	std::vector< std::vector<bool> > isNull;
	int GetRows() {
		return data.size();
	}
	int GetColumns() {
		if(data.size() >= 1)
			return data[0].size();
		return -1;
	}
	DBResult( std::vector< std::vector<std::string> > data, std::vector< std::vector<bool> > isNull ) {
		this->data = data;
		this->isNull = isNull;
	}
};

class cDBStatement  {
friend class cDatabase;
public:
	DBResult execute();
	void bindInt(int idx, int val);
	void bindString(int idx, std::string);
	void bindBlob(int idx, unsigned char* data, int length);
private:
	cDBStatement( sqlite3_stmt* handle ) { stmt = handle; }
	sqlite3_stmt* stmt;
};

class cDatabase  {
friend class cDBStatement;
private:
#if DEBUG
	static long long initmagic;
#endif
	cDatabase(const char* pat);
	cDatabase(cDatabase&); // hide

	bool Open(const char* path);
protected:
	static cDatabase* instance;
	bool ready;
	sqlite3 *dbhandle;

public:
	static std::string ErrorCodeToString(int code);
	static cDatabase* Get();
	static bool Init(const char* path);
	DBResult Query(const char* query);
	cDBStatement* PrepareQuery(const char* query);

	void Close();

};


